package org.carl.component.job.config

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor
import org.carl.component.job.annotation.EnableXxljob
import org.carl.component.job.vo.XxlConfigVo
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.client.RestTemplate


/**
 * email carl.chen@bk.ru
 * @Author: 陈青松
 * @Date: 2019/1/19 2:09
 */
@ConditionalOnBean(annotation = [EnableXxljob::class])
@Configuration
open class XxlJobConfig {
    /**
     * 应用名称
     */
    @Value("\${spring.application.name}")
    val appName:String?=null


    val LOG: Logger = LoggerFactory.getLogger(XxlJobConfig::class.java)

    @Bean
    @ConfigurationProperties("xxl.job")
    open fun xxlConfigVo(): XxlConfigVo {
        val xxlJobConfig=XxlConfigVo();
        return xxlJobConfig;
    }


    @Bean(destroyMethod = "destroy")
    open fun xxlJobExecutor(): XxlJobSpringExecutor {
        LOG.info(">>>>>>>>>>> xxl-job config init.")
        var xxlConfigVo: XxlConfigVo = xxlConfigVo()
        val xxlJobExecutor = XxlJobSpringExecutor()
        xxlJobExecutor.setAdminAddresses(xxlConfigVo.addresses)
        xxlJobExecutor.setAppname(xxlConfigVo.appname?:appName)
        xxlJobExecutor.setIp(xxlConfigVo.ip)
        xxlJobExecutor.setPort(0)
        xxlJobExecutor.setAccessToken(xxlConfigVo.accessToken)
        xxlJobExecutor.setLogPath(xxlConfigVo.logpath)
        xxlJobExecutor.setLogRetentionDays(xxlConfigVo.logretentiondays)
        return xxlJobExecutor
    }
    @Bean
    open fun restTemplate(): RestTemplate{
        return RestTemplate();
    }
}